<template>
  <div class="login">
    <div class="login-from">
      <h2>hello ！</h2>
      <h3>Welcome to my personal website</h3>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="name">
          <el-input
            v-model="ruleForm.name"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="Pass">
          <el-input
            type="password"
            v-model="ruleForm.Pass"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import { getLoginInfo } from "../netWork/login";
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        Pass: "",
        name: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "长度在 3 到 8 个字符",
            trigger: "blur",
          },
        ],
        Pass: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    submitForm() {
      //用户名称  admin   密码： 123456
      if (this.ruleForm.name == "admin" && this.ruleForm.Pass == "123456") {
        //跳转到内部页面  用链式编程跳转
        // this.$router.push("/content");
        this.requestLogin();
      } else {
        this.$message({
          message: "您输入的账号或密码错误！",
          type: "error",
        });
      }
    },
    resetForm() {
      this.ruleForm.name = "";
      this.ruleForm.Pass = "";
    },

    requestLogin() {
      getLoginInfo("/login", {
        Pass: this.ruleForm.name,
        name: this.ruleForm.Pass,
      }).then((res) => {
        // console.log(res); //获取到token
        let routlist = JSON.stringify(res.data.routlist)
        //把token值存到本地存储中（sessionStorage）
        sessionStorage.setItem("token", res.data.token);
        sessionStorage.setItem('routlist',routlist)
        this.$message({
          message: "登陆成功！",
          type: "success",
        });
        this.$router.push("/content");
      });
    },
  },
};
</script>

<style scoped lang="less">
.login {
  height: 100%;
  width: 100%;
  background: url("~@/assets/img/bg.jpg");
  background-size: cover;
  .login-from {
    width: 400px;
    height: 400px;
    position: absolute;
    background-color: rgba(228, 255, 1, 0.2);
    right: 80px;
    bottom: 100px;
  }
}
</style>